﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using KorbeRTR.Models;
using KorbeRTR.Utility;

namespace KorbeRTR
{
    public partial class FormErrorRecord : Form
    {
        public FormErrorRecord()
        {
            InitializeComponent();
        }

        private void FormErrorRecord_Load(object sender, EventArgs e)
        {
            InitGrid(grid1);

            var list = AlarmRecordView.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void InitGrid(FlexCell.Grid grid)
        {
            grid.AutoRedraw = false;
            grid.DisplayRowNumber = true;
            grid.Rows = 1;
            grid.Cols = 7;

            //这个包围区间要减1 居中
            grid.Range(0, 0, grid1.Rows - 1, grid.Cols - 1).Alignment = FlexCell.AlignmentEnum.CenterCenter;

            // 设置行高/列宽
            grid.Column(0).Width = 40;
            grid.Column(1).Width = 160;
            grid.Column(2).Width = 160;
            grid.Column(5).Width = 260;

            // 设置单元格文字
            grid.Cell(0, 0).Text = "序号";
            grid.Cell(0, 1).Text = "开始时间";
            grid.Cell(0, 2).Text = "结束时间";
            grid.Cell(0, 3).Text = "间隔时间";
            grid.Cell(0, 4).Text = "故障代码";
            grid.Cell(0, 5).Text = "故障内容";
            grid.Cell(0, 6).Text = "故障类型";


            grid.AutoRedraw = true;
            grid.Refresh();
        }

        private void UpdateGrid(FlexCell.Grid grid, List<AlarmRecordView> list)
        {
            list.Sort((a, b) => b.StartTime.CompareTo(a.StartTime));

            grid1.AutoRedraw = false;
            grid1.Rows = list.Count + 1;
            grid1.Range(0, 0, grid1.Rows - 1, grid1.Cols - 3).Alignment = FlexCell.AlignmentEnum.CenterCenter;
            grid1.Range(0, 0, grid1.Rows - 1, grid1.Cols - 3).Locked = true;

            for (int i = 0; i < list.Count; i++)
            {
                grid1.Cell(i + 1, 1).Text = list[i].StartTime.ToSqlTime();
                grid1.Cell(i + 1, 2).Text = list[i].EndTime.ToSqlTime();
                grid1.Cell(i + 1, 3).Text = list[i].Duration.ToString();
                grid1.Cell(i + 1, 4).Text = list[i].AlarmCode.Code.ToString();
                grid1.Cell(i + 1, 5).Text = list[i].Content;
                grid1.Cell(i + 1, 6).Text = list[i].AlarmType;
            }

            grid1.AutoRedraw = true;
            grid1.Refresh();
        }
        //检测窗体是否打开
        private bool CheckForm(string formName)
        {
            bool bl = false;
            foreach (Form f in Application.OpenForms)
            {
                if (f.Name == formName)
                {
                    bl = true;
                    f.Activate();
                    break;
                }
            }
            return bl;
        }

        private void ts今天_Click(object sender, EventArgs e)
        {

        }

        private void ts这两天_Click(object sender, EventArgs e)
        {

        }

        private void ts这三天_Click(object sender, EventArgs e)
        {

        }

        private void ts本周_Click(object sender, EventArgs e)
        {

        }

        private void ts本月_Click(object sender, EventArgs e)
        {

        }

        private void ts全部_Click(object sender, EventArgs e)
        {

        }

        private void DTP_Start_ValueChanged(object sender, EventArgs e)
        {
            if (Math.Abs((DTP_Start.Value.Date - DTP_End.Value.Date.AddDays(1)).Days) > 7)
            {
                DTP_End.Value = DTP_Start.Value.Date.Date;
            }

            var list = AlarmRecordView.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void DTP_End_ValueChanged(object sender, EventArgs e)
        {
            if (Math.Abs((DTP_Start.Value.Date - DTP_End.Value.Date.AddDays(1)).Days) > 7)
            {
                MessageBox.Show("查询时间不能超过7天");
                return;
            }

            var list = AlarmRecordView.Select(DTP_Start.Value.Date, DTP_End.Value.Date.AddDays(1));
            UpdateGrid(grid1, list);
        }

        private void tsExit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void tsExport_Click(object sender, EventArgs e)
        {
            if (grid1.ExportToExcel("", true, true))
            {
                MessageBox.Show("输出成功", "导出Excel", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}
